package servlets;

import java.io.IOException;
import java.io.InputStream;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import model.Device;
import model.Teacher;
import service.DeviceService;
import service.TeacherService;

/**
 * Servlet implementation class RegistrationServlet
 */
@WebServlet("/RegistrationServlet")
public class RegistrationServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @EJB
    DeviceService deviceService;
    @EJB
    TeacherService teacherService;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrationServlet() {

        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        InputStream in = request.getInputStream();
        Device device = null;

        try {
            JAXBContext jc = JAXBContext.newInstance(Device.class);
            device = (Device) jc.createUnmarshaller().unmarshal(in);
        }
        catch (JAXBException e) {
            e.printStackTrace();
        }

        if (device != null) {

            int deviceId = deviceService.isRegistered(device);

            // Het device is nog niet geregistreerd, registreer deze.
            if (deviceId == -1) {

                // registreer het device.
                deviceId = deviceService.registerDivice(device);

                // Registreer de benodigde leraren indien nog niet geregistreerd en koppel deze aan het device.
                for (int i = 0; i < device.getTeacherCount(); i++) {

                    Teacher teacher = device.getTeacherAt(i);

                    int teacherId = teacherService.isRegistered(teacher);
                    if (teacherId == -1) {
                        teacherId = teacherService.registerTeacher(teacher);
                    }

                    // Kopel de benodigde leraren aan het device.
                    deviceService.registerLessonForDevice(deviceId, teacherId);
                }
            }
            // Het device staat al in de database, werk het device bij.
            else {

                // Koppel alle leraren los van dit device.
                deviceService.unregisterLessonsForDevice(deviceId);

                // Registreer de benodigde leraren indien nog niet geregistreerd en koppel deze aan het device.
                for (int i = 0; i < device.getTeacherCount(); i++) {

                    Teacher teacher = device.getTeacherAt(i);

                    int teacherId = teacherService.isRegistered(teacher);
                    if (teacherId == -1) {
                        teacherId = teacherService.registerTeacher(teacher);
                    }

                    deviceService.registerLessonForDevice(deviceId, teacherId);
                }
            }
        }
    }
}
